<template>
  <div class="mod-config">
    <div class="funbtn">
      <el-button type="primary" v-if="permissions.mp_wxmessagesend_add" @click="addOrUpdateHandle()">消息发送新增</el-button>
      <el-button type="primary" @click="speakArtHandle">话术管理</el-button>
      <el-button type="primary" @click="messageTemplateHandle">消息模板管理</el-button>
      <el-button type="primary" @click="questionHandle">调查问卷管理</el-button>
      <el-button type="primary" @click="assessHandle">评价管理</el-button>
    </div>

    <el-form inline @keyup.enter.native="searchHandle()">
      <el-form-item label="发送类型" prop="sendType">
        <el-select v-model="dataForm.sendType" placeholder="请选择">
          <el-option :value="1" label="图文形式"></el-option>
          <el-option :value="2" label="消息形式"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发送方式" prop="sendMethod">
        <el-select v-model="dataForm.sendMethod" placeholder="请选择">
          <el-option :value="1" label="立即发送"></el-option>
          <el-option :value="2" label="定时发送"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属公众号" prop="wxAccount">
        <el-select v-model="dataForm.wxAccount" placeholder="请选择">
          <el-option v-for="item in wxAccountOptions" :key="item.account" :label="item.name" :value="item.account"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息发送状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="-1">全部</el-radio>
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="消息创建日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
        <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column prop="sendType" label="发送类型" :formatter="sendTypeFormatter"></el-table-column>
        <el-table-column prop="sendMethod" label="发送方式" :formatter="sendMethodFormatter"></el-table-column>
        <el-table-column prop="wxAccount" label="公众号原始ID"></el-table-column>
        <el-table-column prop="sendObject" label="发送对象" :formatter="sendObjectFormatter"></el-table-column>
        <el-table-column prop="sendInfo" label="发送图文id/发送消息id"></el-table-column>
        <el-table-column prop="status" label="状态" :formatter="statusFormatter"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="permissions.mp_wxmessagesend_edit" type="text" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="permissions.mp_wxmessagesend_del" type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <speak-art v-if="speakArtVisible" ref="speakArtDialog"></speak-art>
    <!-- 消息模板管理 -->
    <message-template v-if="messageTemplateVisible" ref="messageTemplate"></message-template>
    <!-- 调查问卷管理 -->
    <question v-if="questionVisible" ref="questionDialog"></question>
    <!-- 评价管理 -->
    <assess v-if="assessVisible" ref="assessDialog"></assess>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchAccountList } from '@/api/mp/wxaccount'
  import { fetchWxMessageSendList, delWxMessageSendObj } from '@/api/mp/wxmessagesend'

  import TableForm from './wxmessagesend-form'
  import SpeakArt from './components/speakart/index'
  import MessageTemplate from './components/messagetemplate/index'
  import Question from './components/questionnaire/index'
  import Assess from './components/assess/index'

  export default {
    data() {
      return {
        wxAccountOptions: [],
        dataForm: {
          status: -1
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        speakArtVisible: false,
        messageTemplateVisible: false,
        assessVisible: false,
        questionVisible: false
      }
    },
    components: {
      TableForm,
      SpeakArt,
      MessageTemplate,
      Question,
      Assess
    },
    created() {
      this.getDataList()
      this.getAccountList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchWxMessageSendList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 话术管理
      speakArtHandle(){
        this.speakArtVisible = true
        this.$nextTick(() => {
          this.$refs.speakArtDialog.init()
        })
      },
      // 消息模板管理
      messageTemplateHandle() {
        this.messageTemplateVisible = true
        this.$nextTick(() => {
          this.$refs.messageTemplate.init()
        })
      },
      // 调查问卷
      questionHandle() {
        this.questionVisible = true
        this.$nextTick(() => {
          this.$refs.questionDialog.init()
        })
      },
      // 评价管理
      assessHandle() {
        this.assessVisible = true
        this.$nextTick(() => {
          this.$refs.assessDialog.init()
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delWxMessageSendObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      searchHandle() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
        this.dataForm.status = -1
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      //显示转换
      sendTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '图文形式'
            break
          case 2:
            return '消息形式'
            break
        }
      },
      sendMethodFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '立即发送'
            break
          case 2:
            return '定时发送'
            break
        }
      },
      sendObjectFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '所有人'
            break
          case 2:
            return '指定对象'
            break
        }
      },
      statusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '启用'
            break
          case 0:
            return '停用'
            break
        }
      },
      //获取公众号信息
      getAccountList() {
        fetchAccountList().then(result => {
          this.wxAccountOptions = result.data.data
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .funbtn {
    display: flex;
    justify-content: flex-end;
    padding: 10px 20px;
    margin-bottom: 25px;
    background-color: #fff;
  }
</style>
